<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>系统设置 - Powered By SHOP++</title>
<meta name="Author" content="SHOP++ Team" />
<meta name="Copyright" content="SHOP++" />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<script type="text/javascript" src="${ctx}/template/common/js/jquery.tools.js"></script>
<script type="text/javascript" src="${ctx}/template/common/js/jquery.validate.js"></script>
<script type="text/javascript" src="${ctx}/template/common/js/jquery.validate.methods.js"></script>
<script type="text/javascript">
$().ready(function() {

	var $validateErrorContainer = $("#validateErrorContainer");
	var $validateErrorLabelContainer = $("#validateErrorContainer ul");
	var $validateForm = $("#validateForm");
	
	var $smtpFromMail = $("#smtpFromMail");
	var $smtpHost = $("#smtpHost");
	var $smtpPort = $("#smtpPort");
	var $smtpUsername = $("#smtpUsername");
	var $smtpPassword = $("#smtpPassword");
	var $smtpToMailWrap = $("#smtpToMailWrap");
	var $smtpToMail = $("#smtpToMail");
	var $smtpTest = $("#smtpTest");
	var $smtpTestStatus = $("#smtpTestStatus");
	var $subject = $("#subject");
	
	// 邮箱测试
	$smtpTest.click( function() {
		var $this = $(this);
		if ($this.val() == "邮箱测试") {
			$this.val("发送邮件");
			$smtpToMailWrap.show();
		} else {
			if ($.trim($smtpFromMail.val()) == "") {
				$.dialog({type: "warn", content: "请输入发件人邮箱!", modal: true, autoCloseTime: 3000});
				return false;
			}
			if (!/^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test($.trim($smtpFromMail.val()))) {
				$.dialog({type: "warn", content: "发件人邮箱格式错误!", modal: true, autoCloseTime: 3000});
				return false;
			}
			if ($.trim($smtpHost.val()) == "") {
				$.dialog({type: "warn", content: "请输入SMTP服务器地址!", modal: true, autoCloseTime: 3000});
				return false;
			}
			if ($.trim($smtpPort.val()) == "") {
				$.dialog({type: "warn", content: "请输入SMTP服务器端口!", modal: true, autoCloseTime: 3000});
				return false;
			}
			if (!/^[0-9]+$/.test($.trim($smtpPort.val()))) {
				$.dialog({type: "warn", content: "SMTP服务器端口格式错误!", modal: true, autoCloseTime: 3000});
				return false;
			}
			if ($.trim($smtpUsername.val()) == "") {
				$.dialog({type: "warn", content: "请输入SMTP用户名!", modal: true, autoCloseTime: 3000});
				return false;
			}
			if ($.trim($smtpPassword.val()) == "") {
				$.dialog({type: "warn", content: "请输入SMTP密码!", modal: true, autoCloseTime: 3000});
				return false;
			}
			if ($.trim($smtpToMail.val()) == "") {
				$.dialog({type: "warn", content: "请输入收件人邮箱!", modal: true, autoCloseTime: 3000});
				return false;
			}
			if (!/^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test($.trim($smtpToMail.val()))) {
				$.dialog({type: "warn", content: "收件人邮箱格式错误!", modal: true, autoCloseTime: 3000});
				return false;
			}
			
			$.ajax({
				url: "${ctx}/admin/email/sendtest",
				data: {"smtpFromMail": $smtpFromMail.val(), "smtpHost": $smtpHost.val(), "smtpPort": $smtpPort.val(), "smtpUsername": $smtpUsername.val(), "smtpPassword": $smtpPassword.val(), "smtpToMail": $smtpToMail.val(), "subject": $subject.val()},
				type: "POST",
				dataType: "json",
				cache: false,
				beforeSend: function(data) {
					$smtpTestStatus.html('<span class="loadingIcon">&nbsp;</span>正在发送测试邮件,请稍后...');
					$this.attr("disabled", true);
				},
				success: function(data) {
					$smtpTestStatus.empty();
					$this.attr("disabled", false);
					$.dialog({type: data.status, content: data.message, width: 400, ok: "确 定", modal: true});
				}
			});
		}
	});
	
	// 表单验证
	$validateForm.validate({
		errorContainer: $validateErrorContainer,
		errorLabelContainer: $validateErrorLabelContainer,
		wrapper: "li",
		errorClass: "validateError",
		ignoreTitle: true,
		ignore: ".ignoreValidate",
		rules: {
			"email": "email",
			"smtpFromMail": {
				required: true,
				email: true
			},
			"smtpHost": "required",
			"smtpPort": {
				required: true,
				digits: true
			},
			"smtpUsername": "required",
			"subject":"required"
		},
		messages: {
			"smtpFromMail": {
				required: "请填写发件人邮箱",
				email: "发件人邮箱格式错误"
			},
			"smtpHost": "请填写SMTP服务器地址",
			"smtpPort": {
				required: "请填写SMTP服务器端口",
				digits: "SMTP服务器端口必须为零或正整数"
			},
			"smtpUsername": "请填写SMTP用户名",
			"subject": "请填写邮箱主题"
		},
		submitHandler: function(form) {
			$(form).find(":submit").attr("disabled", true);
			form.submit();
		}
	});
	
})
</script>
</head>
<body class="input">
	<div class="bar">
		系统设置
	</div>
	<div id="validateErrorContainer" class="validateErrorContainer">
		<div class="validateErrorTitle">以下信息填写有误,请重新填写</div>
		<ul></ul>
	</div>
	<div class="body">
		<form:form id="validateForm" class="form" modelAttribute="setting" action="${ctx}/admin/email/updatesetting" enctype="multipart/form-data" method="post">
			<table class="inputTable tabContent">
				<tr>
					<th>
						发件人邮箱: 
					</th>
					<td>
						<input type="text" id="smtpFromMail" name="smtpFromMail" class="formText" value="${setting.smtpFromMail}" />
						<label class="requireField">*</label>
					</td>
				</tr>
				<tr>
					<th>
						邮件主题: 
					</th>
					<td>
						<input type="text" id="subject" name="subject" class="formText" value="${setting.subject}" />
						<label class="requireField">*</label>
					</td>
				</tr>
				<tr>
					<th>
						SMTP服务器地址: 
					</th>
					<td>
						<input type="text" id="smtpHost" name="smtpHost" class="formText" value="${setting.smtpHost}" />
						<label class="requireField">*</label>
					</td>
				</tr>
				<tr>
					<th>
						SMTP服务器端口: 
					</th>
					<td>
						<input type="text" id="smtpPort" name="smtpPort" class="formText" value="${setting.smtpPort}" title="默认端口为25" />
						<label class="requireField">*</label>
					</td>
				</tr>
				<tr>
					<th>
						SMTP用户名: 
					</th>
					<td>
						<input type="text" id="smtpUsername" name="smtpUsername" class="formText" value="${setting.smtpUsername}" />
						<label class="requireField">*</label>
					</td>
				</tr>
				<tr>
					<th>
						SMTP密码: 
					</th>
					<td>
						<input type="password" id="smtpPassword" name="smtpPassword" value="" class="formText" title="留空则不进行密码修改" />
					</td>
				</tr>
				<tr>
					<th>
						邮箱配置测试: 
					</th>
					<td>
						<span id="smtpToMailWrap" class="hidden">
							<div>收件人邮箱: </div>
							<input type="text" id="smtpToMail" name="smtpToMail" value="378394705@qq.com" class="formText" />
						</span>
						<input type="button" id="smtpTest" class="formButton" value="邮箱测试" hidefocus />
						<span id="smtpTestStatus"></span>
					</td>
				</tr>
			</table>
			<div class="buttonArea">
				<input type="submit" class="formButton" value="确  定" hidefocus />&nbsp;&nbsp;
				<input type="button" class="formButton" onclick="window.history.back(); return false;" value="返  回" hidefocus />
			</div>
		</form:form>
	</div>
</body>
</html>